<template>
	<view class="content">
		<radio-group>
			<view class="content-top" v-if="list.length">
				<view class="uni-list-cell uni-list-cell-pd" style="overflow: hidden;margin: 10rpx 30rpx;"
					v-for="(item, key) in list" :key="key">
					<view class='cell-group min-cell-group'
						style="background-color: #202226;border-radius: 20rpx;padding:35rpx 25rpx 25rpx 25rpx;">
						<view class='cell-item' style="background-color: transparent;">
							<view class='cell-item-hd' @click="isSelect(item)">
								<view class='cell-hd-title'
									style="color: #fff;font-size: 27rpx;font-weight: bold;margin-bottom: 17rpx;">
									{{item.name}} <text class="phone-num">{{item.mobile}}</text>
								</view>
							</view>
						</view>
						<view class='cell-item' @click="isSelect(item)"
							style="padding-bottom: 27rpx;background-color: transparent;border-bottom: 1rpx solid #d4d4d4;">
							<view class='cell-item-hd'>
								<view class="cell-bd-view">
									<text class="cell-bd-text"
										style="color: #999;font-size: 25rpx;">{{item.area_name + item.address}}</text>
								</view>
							</view>
						</view>
						<view class='cell-item-ft'
							style="display: flex;justify-content: space-between;align-items: center;padding-top: 20rpx;">
							<view class="Left" style="display: flex;align-items: center;">
								<view>
									<radio :checked="item.is_def===1" :disabled="true" />
								</view>
								<text v-if="item.is_def===1" style="color: #F2D7AAFF;">默认地址</text>
								<text v-else style="color: #B9B9B9FF;">默认地址</text>
							</view>
							<view class="Right" style="display: flex;">
								<view class="A1" style="display: flex;align-items: center;" @click="toEdit(item.id)">
									<image class='cell-ft-next icon' src='/static/bj.png'
										style="width: 24rpx;height: 23rpx;margin-right: 7rpx;">
									</image>
									<text class="cell-ft-text">编辑</text>
								</view>
								<view class="A2" style="display: flex;align-items: center;margin-left: 20rpx;"
									v-if="item.is_def!==1">
									<image class='cell-ft-next icon'
										style="width: 24rpx;height: 23rpx;margin-right: 7rpx;" src='/static/bj.png'
										@click="toEdit(item.id)">
									</image>
									<text class="cell-ft-text" @click="delShip(item.id)">删除</text>
								</view>
							</view>
						</view>
						<view v-show="item.is_def==1" class="desc-card">{{address_desc}}</view>
					</view>
				</view>
			</view>
			<view class="address-none" v-else>
				<image class="address-none-img" src="/static/image/order.png" mode=""></image>
			</view>
			<view class="button-bottom">
				<button
					style="width: 580rpx;height: 80rpx;background: #F2D7AAFF;border-radius: 10rpx 10rpx 10rpx 10rpx;font-size: 32rpx;color: #000;"
					@click="toAdd()" hover-class="btn-hover2">添加地址</button>
			</view>
		</radio-group>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [], // 用户收货地址列表
				type: ''
			}
		},
		onLoad(e) {
			if (e.type) {
				this.type = e.type;
			}
		},
		computed: {
			
			address_desc() {
				return this.$store.state.config.address_desc;
			}
			
		},
		onShow() {
			this.userShipList();
		},
		methods: {
			// 获取收货地址列表
			userShipList() {
				this.$api.userShip({}, res => {
					if (res.status) {
						this.list = res.data

					}
				})
			},
			// 收货地址删除
			delShip(id) {
				this.$common.modelShow('提示', '确认删除此收货地址?', () => {
					let data = {
						id: id
					}
					this.$api.removeShip(data, res => {
						if (res.status) {
							this.$common.successToShow(res.msg, () => {
								this.userShipList();
							});
						} else {
							this.$common.errorToShow(res.msg);
						}
					})
				})
			},
			//编辑
			toEdit(id) {
				this.$common.navigateTo('./index?ship_id=' + id);
			},
			//添加
			toAdd() {
				this.$common.navigateTo('./index');
			},
			//选择
			isSelect(data) {
				if (this.type == 'order') {
					// 		let pages = getCurrentPages();//当前页
					// 		let beforePage = pages[pages.length - 2];//上个页面

					// 		// #ifdef MP-ALIPAY || MP-TOUTIAO
					// 		this.$db.set('address_user_ship', data, true);
					// 		// #endif

					// 		// #ifdef H5 || APP-PLUS || APP-PLUS-NVUE
					// 		this.$store.commit("userShip",data)
					// 		// #endif

					// 		// #ifdef MP-WEIXIN
					// 		beforePage.$vm.userShip = data;
					// 		beforePage.$vm.params.area_id = data.area_id;
					// 		// #endif

					this.$db.set('address_user_ship', data, true);
					uni.navigateBack({
						delta: 1
					});
					// this.$common.navigateTo("/pages/goods/place-order/index")
				}
			},
			// #ifdef MP-WEIXIN
			wechatAddress: function() {
				wx.chooseAddress({
					success: res => {
						if (res.errMsg == "chooseAddress:ok") {
							//获取成功
							//存储这个收获地区信息到数据库
							let data = {
								province_name: res.provinceName,
								city_name: res.cityName,
								county_name: res.countyName,
								postal_code: res.postalCode
							};
							let areaId = 0;
							this.$api.getAreaId(data, res1 => {
								if (res1.status) {
									//存储用户收货信息
									let userShipId = 0;
									// let userShipData = {
									// 	area_id: res1.data,
									// 	user_name: res.userName,
									// 	detail_info: res.detailInfo,
									// 	tel_number: res.telNumber,
									// 	is_def: 2
									// }
									let userShipData = {
										area_id: res1.data,
										name: res.userName,
										address: res.detailInfo,
										mobile: res.telNumber,
										is_def: 2
									}
									this.$api.editShip(userShipData, res2 => {
										if (res2.status) {
											this.$common.errorToShow('存储微信地址成功', r => {
												setTimeout(rp => {
													this.userShipList();
												}, 1000);
											});
										} else {
											uni.showModal({
												title: '提示',
												content: '存储微信地址失败',
												showCancel: false
											});
										}
									});
								} else {
									uni.showModal({
										title: '提示',
										content: '获取微信地址失败',
										showCancel: false
									});
								}
							});
						} else {
							uni.showModal({
								title: '提示',
								content: '获取微信地址失败',
								showCancel: false
							});
						}
					}
				});
			},
			// #endif
		}
	}
</script>

<style>
	.desc-card{
		color: red;
		font-size: 24rpx;
		padding-top: 10rpx;
	}
	::v-deep uni-radio .uni-radio-input.uni-radio-input-disabled:before {
		color: #000 !important;
	}

	.cell-tip {
		background-color: #FF7159;
		color: #fff;
		font-size: 24upx;
		display: inline-block;
		float: left;
		padding: 4upx 10upx;
		margin-right: 10upx;
		transform: scale(.9);
	}

	.min-cell-group .cell-ft-text {
		font-size: 24upx;
		margin-right: 10upx;
		color: #B9B9B9FF;
	}

	.min-cell-group .cell-item-bd {
		color: #666;
		padding-right: 0;
	}

	.min-cell-group .default {
		color: #666;
	}

	.min-cell-group uni-radio .uni-radio-input {
		width: 36upx;
		height: 36upx;
	}

	.min-cell-group .default .checked-radio {
		display: inline-block;
		float: left;
		position: relative;
		bottom: 2upx;
	}

	.green {
		background-color: #999;
	}

	.cell-hd-title {
		font-size: 28upx;
	}

	.phone-num {
		margin-left: 20upx;
	}

	.address-none {
		text-align: center;
		padding: 200upx 0;
	}

	.address-none-img {
		width: 274upx;
		height: 274upx;
	}
</style>